package com.example.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.junit.Assert.*;
import java.util.List;

public class MovieListPage extends InternalPage{

    @FindBy(xpath = "//section/nav/ul/li[1]//a")
    public WebElement addMovie;

    @FindBy(css = "div.nocover")
    public WebElement movie;

    @FindBy(className = "content")
    public WebElement listMovieEmpty;

    @FindBy(css = "div.title")
    public WebElement listMovie;

    @FindBy (css = "div#results > a")
    public List<WebElement> linksToFilms;

    public boolean isOnThisPage(){
        return menuLogoutLink.isDisplayed();
    }

    @Override
    void tryToOpen(){
        MyPageFactory.getPage(driver, LoginPage.class)
            .loginWithValidCredentials("admin", "admin")
            .gotoHomePage();
    }

    public MoviePage gotoFilm(String nameFilm){
        for(WebElement links : linksToFilms){
            WebElement linkFilm = links.findElement(By.xpath("//img[@title='" + nameFilm + "' ]"));
            if(linkFilm.isDisplayed()){
                linkFilm.click();
            }
        }
        return MyPageFactory.getPage(driver, MoviePage.class);
    }

    public MovieAddPage addNewMovie(){
        addMovie.click();
        return MyPageFactory.getPage(driver, MovieAddPage.class);
    }

    public MovieListPage searchMoviesPage() {
        assertEquals("Фильм не удален!", "No movies where found.", listMovieEmpty.getText());
        return MyPageFactory.getPage(driver, MovieListPage.class);
    }

    public MoviePage gotoMovie(){
        movie.click();
        return MyPageFactory.getPage(driver, MoviePage.class);
    }

    public MovieListPage presentMovie(String movieName) {
        assertEquals("Фильм не удален!", movieName,  listMovie.getText());
        return MyPageFactory.getPage(driver, MovieListPage.class);
    }
}
